package com.dudu.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.dudu.bean.PoolAccountRelBean;
import com.dudu.dao.PoolAccountRelDao;
import com.dudu.model.PoolAccountRel;
import com.dudu.service.IPoolAccountRelService;
import com.dudu.utils.EnumUtility;

import core.CoreDao;
import core.CoreDaoInvoke;

public class PoolAccountRelServiceImpl implements IPoolAccountRelService {
	// 日志输出
	private PoolAccountRelDao poolDao=new PoolAccountRelDao();
	private CoreDao coreDao = CoreDaoInvoke.getInstance();
	
	/**
	 * 根据账号id和邮件通知，更新该账号下所有rel关系中邮件通知内容
	 * @param cust_account 账号标识
	 * @param app_key 应用标识
	 * @param value 邮件通知或主动切换号码的值
	 * @param type 判断是邮件通知，还是主动切换号码
	 * @return
	 */
	@Override
	public int updateEmailSwitchByAccId(String cust_account,String app_key, String value, String type){
		int result = -1;
		// 主动切换号码
		if(type.equals("switchNum"))
			result = poolDao.updateSwitchNumByAccId(coreDao, cust_account, app_key, value);
		else
			// 邮件通知
			result = poolDao.updateEmailNoticeByAccId(coreDao, app_key, cust_account, value);
		return result;
	}
	/**
	 * 查询rel表中当前账户的rel关系中第一条数据的email_notice，switch_num值
	 * @param cust_account 账号标识
	 * @param app_key 应用 标识
	 * @return
	 */
    public JSONObject getEmailSwitchByAccoutId(String app_key,String cust_account){
		JSONObject object = poolDao.getEmailSwitchByAccoutId(coreDao, app_key, cust_account);
		return object;
	}
    /**
     * 删除能力
     * @param app_key 应用标识
     * @param cust_account 账号标识
     * @param abilityId  能力ID
     * @return
     */
    public int deleteByAccAbiId(String app_key, String cust_account,String abilityId){
    	return poolDao.deleteByAccAbiId(coreDao, app_key, cust_account, abilityId);
    }
    /**
     * 插入账号与号码能力关系信息
     * @param poolAccountRel
     * @param dao
     * @return
     */
    public int doInsert(PoolAccountRelBean poolAccountRel){
    	return poolDao.insert(coreDao, conversion(poolAccountRel));
    }
    /**
	 * 根据账号id查询所有账号与号码能力关系信息，并按照能力类型分别统计个数
	 * @param dao
	 * @param app_key
	 * @param cust_account
	 * @return
	 */
    public List<PoolAccountRelBean> getTotalRelByAccoutId(String app_key,String cust_account){
    	//后面所需返回的list
    	List<PoolAccountRelBean> result=new ArrayList<PoolAccountRelBean>();
    	//开始查询账号信息
    	List<PoolAccountRel> poolAccountRel=poolDao.getTotalByAccoutId(coreDao, cust_account, app_key);//.getTotalByAccoutId(coreDao, app_key,cust_account);
    	if(EnumUtility.isBlank(poolAccountRel)){
    		return null;
    	}else{
    		for(PoolAccountRel pool:poolAccountRel){
    			result.add(conversion(pool));
    		}
    		return result;
    	}
    }
    /**
	 * 转化
	 * @param menu
	 * @return
	 */
	private PoolAccountRelBean conversion(PoolAccountRel bean){
		PoolAccountRelBean poolBean=new PoolAccountRelBean();
		BeanUtils.copyProperties(bean, poolBean);//源,目的
		//PropertyUtils.copyProperties(dest, orig);//目的,源
		return poolBean;
	}
	/**
	 * 转化
	 * @param menuBean
	 * @return
	 */
	private PoolAccountRel conversion(PoolAccountRelBean bean){
		PoolAccountRel pool=new PoolAccountRel ();
		BeanUtils.copyProperties(bean,pool);//源,目的
		//PropertyUtils.copyProperties(dest, orig);//目的,源
		return pool;
	}
}
